﻿using System;
using System.ComponentModel;
using System.Data;
using System.Web.Services;
using System.Configuration;
using System.DirectoryServices;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Shuba.Model;

namespace Shuba.Web.ws
{
    /// <summary>
    /// Summary description for wsEnh1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class wsEnh1 : System.Web.Services.WebService
    {
        static Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();
        static string DBServer = ConfigurationManager.AppSettings["db.server"];
        static string DBName = ConfigurationManager.AppSettings["db.name"];
        static string DBUser = ConfigurationManager.AppSettings["db.user"];
        static string DBPass = Encryption.Decrypt(ConfigurationManager.AppSettings["db.pwd"]);
        static string cn = String.Format("Data Source = {0}; Initial Catalog = {1}; User Id = {2}; Password = {3}; Connect Timeout = 30; Min Pool Size = 5", DBServer, DBName, DBUser, DBPass);
        static DbProviderFactory provider = DbProviderFactories.GetFactory("System.Data.SqlClient");
        static Shuba.Framework.Encryption enc = new Shuba.Framework.Encryption();
        string AppCode = ConfigurationManager.AppSettings["app.code.spada"];
        static string SPADADBEncCode = enc.Decrypt(ConfigurationManager.AppSettings["enc.code.spada"]);

        private static Database CreateDatabase()
        {
            return new GenericDatabase(cn, provider);
        }

        //private void AppendToLog(string log)
        //{
        //    System.Web.HttpContext.Current.Response.AppendToLog(log + " " + DateTime.Now.ToString("dd MMM yyyy hh:mm:ss:fff"));
        //}

        private UserInfo Get(string nip, string AppCode)
        {
            UserInfo user = new UserInfo();
            Database db = DBConn.CreateDatabase();
            DbCommand cmd;

            string sql = @"EXEC GET_ALL_USER @NIP, @APP_ID";
            cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@NIP", DbType.String, nip);
            db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    user = new UserInfo(dr.GetInt32(0), dr.GetString(1).Trim(), dr.GetString(2), dr.GetString(3), dr.GetString(4), dr.GetString(5), dr.GetString(6), dr.GetInt32(7), dr.GetString(8) == "1" ? true : false, dr.GetString(9) == "1" ? true : false, dr.GetDateTime(10), dr.GetString(11), dr.GetString(12));
                }
                dr.Close();
            }
            return user;
        }

        [WebMethod]
        public void AddLog(string NIP, string IPAddress, bool isLogin)
        {
            try
            {
                Database db = DBConn.CreateDatabase();
                DbCommand cmd;
                if (isLogin)
                {
                    UserInfo info = Get(NIP, AppCode);
                    info.IsLogin = true;
                    info.LoginAddr = IPAddress;
                    info.LoginDate = DateTime.Now;

                    //update user
                    cmd = db.GetStoredProcCommand("USER_UPDATE");
                    db.AddInParameter(cmd, "@USER_ID", DbType.Int32, info.Id);
                    db.AddInParameter(cmd, "@APP_ID", DbType.String, info.AppId);
                    db.AddInParameter(cmd, "@IS_LOGIN", DbType.String, "1");
                    db.AddInParameter(cmd, "@LOGIN_ADDRESS", DbType.String, info.LoginAddr);

                    db.ExecuteNonQuery(cmd);

                    //add translog

                    cmd = db.GetStoredProcCommand("ADD_TRANS_LOG");
                    db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
                    db.AddInParameter(cmd, "@USER_NAME", DbType.String, NIP);
                    db.AddInParameter(cmd, "@TRANS_TYPE", DbType.String, "Login");
                    db.AddInParameter(cmd, "@TRANS_DESC", DbType.String, String.Format("Login To {0} Application, User Name : {1}, Remote Address: {2}", AppCode, NIP, IPAddress));

                    db.ExecuteNonQuery(cmd);
                }
                else
                {
                    //update user
                    UserInfo info = Get(NIP, AppCode);
                    cmd = db.GetStoredProcCommand("USER_UPDATE");
                    db.AddInParameter(cmd, "@USER_ID", DbType.Int32, info.Id);
                    db.AddInParameter(cmd, "@APP_ID", DbType.String, info.AppId);
                    db.AddInParameter(cmd, "@IS_LOGIN", DbType.String, "0");
                    db.AddInParameter(cmd, "@LOGIN_ADDRESS", DbType.String, info.LoginAddr);

                    db.ExecuteNonQuery(cmd);

                    //add translog
                    cmd = db.GetStoredProcCommand("ADD_TRANS_LOG");
                    db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
                    db.AddInParameter(cmd, "@USER_NAME", DbType.String, NIP);
                    db.AddInParameter(cmd, "@TRANS_TYPE", DbType.String, "Logout");
                    db.AddInParameter(cmd, "@TRANS_DESC", DbType.String, String.Format("Logout To {0} Application, User Name : {1}, Remote Address: {2}", AppCode, NIP, IPAddress));

                    db.ExecuteNonQuery(cmd);
                }
            }
            catch (Exception ex) 
            {
                CreateErrorLog("wsEnh1 AddLog : " + ex.Message.ToString(), true);
            }
        }
        
        [WebMethod]
        public string CheckDuplicateData(string FullName, string BusinessAddress)
        {   
            Database db = CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand("CHECK_DEDUP_DATA");
            cmd.CommandTimeout = 3600;

            db.AddInParameter(cmd, "@FULL_NAME", DbType.String, FullName);
            db.AddInParameter(cmd, "@ADDRESS", DbType.String, BusinessAddress);

            return db.ExecuteScalar(cmd).ToString();
        }
        
        [WebMethod]
        public string CheckLDAPSPADAUserNew(string LoginUser, string LoginPass)
        {
            string UserUnitCode = "~";

            if (IsLDAPUserNew(LoginUser, LoginPass))
            {
                Database db = DBConn.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand("SELECT TOP 1 LTRIM(RTRIM(BRANCH_ID)) FROM V_USER (NOLOCK) WHERE (LTRIM(RTRIM(APP_ID)) = @APP_ID) AND (LTRIM(RTRIM(NIP)) = @NIP) ;");

                db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);
                db.AddInParameter(cmd, "@NIP", DbType.DateTime, LoginUser);

                UserUnitCode = db.ExecuteScalar(cmd).ToString() + "," + SPADADBEncCode;

                return UserUnitCode;
            }
            else
            {
                return UserUnitCode + "," + SPADADBEncCode;
            }
        }
        
        [WebMethod]
        public bool CheckService()
        {
            return true;
        }

        [WebMethod]
        public string GeneratePRNNew(string unitCode, string segmentCode, string prospectType, string debtor_type, string channeling, string CustName, string IsNegativeChecking)
        {
            string prn = "";

            try
            {
                Database db = DBConn.CreateDatabase();
                DbCommand cmd = db.GetStoredProcCommand("GENERATE_PRN_NEW");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unitCode.Trim());
                db.AddInParameter(cmd, "@SEGMENT_CODE", DbType.String, segmentCode.Trim());
                db.AddInParameter(cmd, "@PROSPECT_TYPE", DbType.String, prospectType.Trim());
                db.AddInParameter(cmd, "@DEBTOR_TYPE", DbType.String, debtor_type.Trim());
                db.AddInParameter(cmd, "@CHANNEL_ID", DbType.String, channeling.Trim());
                db.AddInParameter(cmd, "@NAM_CUST", DbType.String, CustName);
                db.AddInParameter(cmd, "@IS_CHECK_NEGATIVE_FILE", DbType.String, IsNegativeChecking.Trim());

                cmd.CommandTimeout = 1800;
                prn = db.ExecuteScalar(cmd).ToString();
            }
            catch (Exception ex)
            {
                CreateErrorLog("wsEnh1 GeneratePRNNew : " + ex.Message.ToString(), true);
            }
            return prn;
        }

        [WebMethod]
        public byte[] GetBranchByCode(string nip, string pass, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand("SELECT BRANCH_ID, BRANCH_NAME FROM TBL_MS_BRANCH (NOLOCK) WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetBranchNIPSO(string nip, string pass, DateTime update, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand("SELECT BRANCH_ID, AO_CODE, NIP FROM TBL_MS_BRANCH_NIP_SO (NOLOCK) WHERE (LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE) AND (IS_DOWNLOADED = '0') AND (UPDATE_DATE > @UPDATE_DATE) ;");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);
                db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, update);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetBranchPlan(string nip, string pass, string unit, DateTime update)
        {   
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();

                DbCommand cmd = db.GetSqlStringCommand(@"SELECT BRANCH_ID, TARGET_YEAR, TARGET_MONTH, TARGET_AMOUNT, NOA, TARGET_AMOUNT_REAL, NOA_REAL 
		                                                 FROM TBL_BRANCH_TARGET_PLAN  (NOLOCK) 
		                                                 WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE AND (UPDATE_DATE > @UPDATE_DATE) ;");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);
                db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, update);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetBranchPlanAll(string nip, string pass, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();

                DbCommand cmd = db.GetSqlStringCommand(@"SELECT BRANCH_ID, TARGET_YEAR, TARGET_MONTH, TARGET_AMOUNT, NOA, TARGET_AMOUNT_REAL, NOA_REAL 
	                                                     FROM TBL_BRANCH_TARGET_PLAN (NOLOCK) 
	                                                     WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE ;");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);
                
                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetBranchs(string nip, string pass, DateTime updateDate, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT BRANCH_ID, SOURCE_CODE, BRANCH_MAPPING, BRANCH_NAME, PHONE_NO 
		                                                 FROM TBL_MS_BRANCH (NOLOCK)  
		                                                 WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE AND (UPDATE_DATE > @UPDATE_DATE) ;");
                db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, updateDate);
                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);
                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetBranchZIP(string nip, string pass, DateTime updateDate, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"IF EXISTS (
					                                            SELECT BRANCH_ID, ZIP_CODE, KODYA_KAB, KECAMATAN, 
						                                            KELURAHAN, LAYER, QUADRANT, IS_DELETED 
					                                            FROM TBL_MS_BRANCH_ZIP (NOLOCK)  
					                                            WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE 
						                                            AND (UPDATE_DATE > @UPDATE_DATE)  
				                                              )
                                                    SELECT BRANCH_ID, ZIP_CODE, KODYA_KAB, KECAMATAN, KELURAHAN, LAYER, QUADRANT, IS_DELETED
		                                            FROM TBL_MS_BRANCH_ZIP (NOLOCK) WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE");
                db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, updateDate);
                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetBranchZIPAll(string nip, string pass, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT BRANCH_ID, ZIP_CODE, KODYA_KAB, KECAMATAN, KELURAHAN, LAYER, QUADRANT, IS_DELETED
		                                                 FROM TBL_MS_BRANCH_ZIP (NOLOCK) WHERE LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod(CacheDuration=300)]
        public byte[] GetChannel(string nip, string pass)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT CHANNEL_ID, CHANNEL_NAME, IS_ACTIVE FROM TBL_MS_CHANNEL (NOLOCK)");

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetDocumentProduct(string nip, string pass, DateTime updateDate)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"IF EXISTS (SELECT PRODUCT_ID, DOCUMENT_ID 
		                                                FROM TBL_DOCUMENT_PRODUCT (NOLOCK) WHERE (UPDATE_DATE > @UPDATE_DATE))
                                                        SELECT PRODUCT_ID, DOCUMENT_ID FROM TBL_DOCUMENT_PRODUCT ");

                db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, updateDate);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod(CacheDuration=300)]
        public byte[] GetDocuments(string nip, string pass)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT DOCUMENT_ID, DOCUMENT_NAME, IS_ACTIVE FROM TBL_MS_DOCUMENTS (NOLOCK)");

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod(CacheDuration=300)]
        public byte[] GetProducts(string nip, string pass)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT PRODUCT_ID, PRODUCT_NAME, PRODUCT_TYPE, IS_ACTIVE FROM TBL_MS_PRODUCTS (NOLOCK)");

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public DateTime GetServerDateTime()
        {
            return DateTime.Now;
        }
    
        [WebMethod]
        public byte[] GetSPADAMainMenu(string nip, string pass)
        {
            if (IsValidUser(nip, pass))
            {
                string SPADAAppCode = ConfigurationManager.AppSettings["app.code.spada"];

                Database db = CreateDatabase();
                DbCommand cmd = db.GetStoredProcCommand("GET_SPADA_USER_MAIN_MENU");

                db.AddInParameter(cmd, "@APP_ID", DbType.String, SPADAAppCode);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetSpadaUpdate()
        {
            Database db = CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(@"SELECT [DATE_FILE_UPLOAD],[DATE_FILE_MODIFIED]
                                                              ,[FILE_SIZE_UPLOAD],[PATH_FILE_UPLOAD]
                                                              ,[NAME_FOLDER_UPLOAD],[FILE_NAME_UPLOAD]
                                                              ,[ENTRY_BY],[ENTRY_DATE]
                                                              ,[UPDATE_BY],[UPDATE_DATE]
                                                        FROM TBL_MS_SPADA_FILE_VERSION (NOLOCK) 
                                                        WHERE [UPDATE_DATE] = (SELECT MAX([UPDATE_DATE]) 
                                                                                FROM TBL_MS_SPADA_FILE_VERSION (NOLOCK) AS F 
                                                                                WHERE SUBSTRING(F.[FILE_NAME_UPLOAD], 1, CHARINDEX('~', F.[FILE_NAME_UPLOAD]) - 1) = SUBSTRING(TBL_MS_SPADA_FILE_VERSION.[FILE_NAME_UPLOAD], 1, CHARINDEX('~', TBL_MS_SPADA_FILE_VERSION.[FILE_NAME_UPLOAD]) - 1));");

            return TransformIntoBytes(db.ExecuteDataSet(cmd));
        }

        [WebMethod]
        public byte[] GetSPADAUserMenu(string nip, string pass, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                string SPADAAppCode = ConfigurationManager.AppSettings["app.code.spada"];

                Database db = CreateDatabase();
                DbCommand cmd = db.GetStoredProcCommand("GET_SPADA_USER_MENU");

                db.AddInParameter(cmd, "@APP_ID", DbType.String, SPADAAppCode);
                db.AddInParameter(cmd, "@NIP", DbType.String, nip);
                db.AddInParameter(cmd, "@BRANCH_ID", DbType.String, unit);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetStandard(string nip, string pass, DateTime update)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT MS_STD_ID, MS_STD_GRP_ID, MS_STD_NAME, IS_ACTIVE, MS_STD_TYPE 
		                                                 FROM TBL_MS_STANDARD (NOLOCK) WHERE UPDATE_DATE > @UPDATE_DATE");

                db.AddInParameter(cmd, "@UPDATE_DATE", DbType.DateTime, update);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetSysParams(string nip, string pass)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(@"SELECT TOP 1 ROW_ID, GU_FUP, GU_CLOSE_DEAL, PURGING_PERIOD, WEB_ROOT_ADDRESS, 
			                                                DB_PASS_NEW, DB_PASS_OLD, NPWP, TIME_OUT 
		                                                FROM TBL_SYS_PARAMS (NOLOCK) ORDER BY UPDATE_DATE DESC");

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetUpdateTableSpada(string createDate)
        { 
            Database db = CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(@"SELECT [TABLE_NAME]
                                  ,[FIELD_NAME]
                                  ,[DATA_TYPE]
                                  ,[IS_TABLE_NEW]
                                  ,[ENTRY_DATE]
                              FROM [TBL_UPDATE_TBL_SPADA] (NOLOCK) WHERE ENTRY_DATE > DATEADD(DAY, -10, @ENTRY_DATE)");

            db.AddInParameter(cmd, "@ENTRY_DATE", DbType.DateTime, createDate);

            return TransformIntoBytes(db.ExecuteDataSet(cmd));
        }
        
        [WebMethod]
        public byte[] GetUsers(string nip, string pass, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();

                DbCommand cmd = db.GetSqlStringCommand(@"SELECT USER_ID, LTRIM(RTRIM(AO_CODE)) AS AO_CODE, NIP, BRANCH_ID, USER_NAME, 
		                                                       USER_TYPE, EMAIL_ADDRESS, MAC_ADDRESS, OLD_MAC_ADDRESS, IS_MIGRATION 
	                                                    FROM V_USER (NOLOCK) WHERE (LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE) AND (APP_ID = @APP_ID) ");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);
                db.AddInParameter(cmd, "@APP_ID", DbType.String, AppCode);

                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }

        [WebMethod]
        public byte[] GetUsersCO(string nip, string pass, string unit)
        {
            if (IsValidUser(nip, pass))
            {
                Database db = CreateDatabase();

                DbCommand cmd = db.GetSqlStringCommand(@"SELECT USER_ID, LTRIM(RTRIM(AO_CODE)) AS AO_CODE, NIP, BRANCH_ID, 
			                                                    USER_NAME, USER_TYPE, EMAIL_ADDRESS, MAC_ADDRESS, OLD_MAC_ADDRESS 
		                                                 FROM V_USER (NOLOCK) WHERE (LTRIM(RTRIM(BRANCH_ID)) = @UNIT_CODE) 
				                                                AND (LTRIM(RTRIM(USER_TYPE)) ='CO') 
		                                                 GROUP BY USER_ID, AO_CODE, NIP, BRANCH_ID, USER_NAME, USER_TYPE, EMAIL_ADDRESS, 
				                                                MAC_ADDRESS, OLD_MAC_ADDRESS ");

                db.AddInParameter(cmd, "@UNIT_CODE", DbType.String, unit);
                return TransformIntoBytes(db.ExecuteDataSet(cmd));
            }
            return null;
        }
        
        static bool IsLDAPUserNew(string LDAPUser, string LDAPPass)
        {
            if (basePage.ByPassAD())
                return true; //FOR TESTING PURPOSE ONLY

            string LDAPServer = ConfigurationManager.AppSettings["ldap"];

            DirectoryEntry entry = new DirectoryEntry(LDAPServer, LDAPUser, LDAPPass);
            try
            {
                string EName = entry.Name; //Untuk Trap Error (LDAP tidak dapat) atau berhasil

                //System.Web.HttpContext.Current.Response.AppendToLog("end IsLDAPUserNew " + LDAPUser + DateTime.Now.ToString("dd MMM yyyy hh:mm:ss:fff"));
                return true;
            }
            catch
            {
                return false;
            }
        }

        static bool IsValidUser(string LDAPUser, string LDAPPass)
        {
            return true;
        }
        
        private byte[] TransformIntoBytes(System.Data.DataSet tables)
        {
            if (tables == null) return null;
            using (MemoryStream mem = new MemoryStream())
            {
                tables.WriteXml(mem, System.Data.XmlWriteMode.WriteSchema);
                byte[] result = mem.ToArray();
                if (result != null)
                {
                    if (result.Length > 0)
                    {
                        return result;
                    }
                }
            }
            return null;
        }

        void CreateErrorLog(string text, bool isnewline)
        {
            try
            {
                string appPath = Server.MapPath("~/AppLog");
                if (appPath.Substring(appPath.Length - 1, 1) == "\\")
                {
                    appPath = appPath.Substring(0, appPath.Length - 1);
                }

                if (File.Exists(String.Format("{0}\\Log.txt", appPath)))
                {
                    using (StreamWriter file = new StreamWriter(String.Format("{0}\\Log.txt", appPath), true))
                    {
                        if (!isnewline) file.WriteLine();
                        file.WriteLine(String.Format("{0}", text));
                    }
                }
                else
                {
                    File.WriteAllText(String.Format("{0}\\Log.txt", appPath), text);
                }
            }
            catch (Exception) { }
        }
    }
}